<?php


class InsertUser extends Connection{
	private $conn;

	public function __construct(){
		Connection::init();
		$this->conn = $this->getConnection();
	}

	public function execute(User $obj){

			$companies = $obj->getCompanies();

			$shops = $companies[0]->getShops();
			//take status id
			$stmt = $this->conn->prepare("SELECT  `id`
										FROM  `user_status`
										WHERE  `name` =  ? ");

			$stmt->bindValue(1,$obj->getStatus(),PDO::PARAM_STR);
			if (!$stmt->execute()){
				throw new Exception ("не мога да взема статус Id");
			};
			$status = $stmt->fetchColumn();
			//take privileges id
			$stmt = $this->conn->prepare("SELECT  `id`
										FROM `user_privileges`
										WHERE `privileges` =  ?");

			$stmt->bindValue(1,$obj->getPrivileges(),PDO::PARAM_STR);
			if (!$stmt->execute()){
				throw new Exception ("не мога да взема прив Id");
			};
			$priv = $stmt->fetchColumn();



			//add user
			$pass = hash("sha512",$obj->getPassword());
			$stmt = $this->conn->prepare("INSERT INTO `user`( `first_name`, `last_name`, `email`, `password`, `new_password`, `status_id`, `privileges_id`)
			                                       VALUES (?,?,?,?,?,?,?)");

			$stmt->bindValue (1,$obj->getFirstName());
			$stmt->bindValue (2,$obj->getLastName());
			$stmt->bindValue (3,$obj->getEmail());
			$stmt->bindValue (4,$pass);
			$stmt->bindValue (5,$obj->getNewPassword());
			$stmt->bindValue (6,$status);
			$stmt->bindValue (7,$priv);
			if (!$stmt->execute()){
				throw new Exception('не мога да добавя User');
			};

			//add company
			$stmt = $this->conn->prepare("INSERT INTO `company`(`name`, `address`, `Resp_official`)
			                                     VALUES (?,?,?)");
			$stmt->bindValue (1,$companies[0]->getName());
			$stmt->bindValue (2,$companies[0]->getAddress());
			$stmt->bindValue (3,$companies[0]->getRespOfficial());
			if (!$stmt->execute()){
				throw new Exception ("не мога да добавя Company");
			};

			//take user id
			$stmt = $this->conn->prepare("SELECT  `id`
										FROM  `user`
										WHERE  `email` =  ?");
			$stmt->bindValue(1,$obj->getEmail());
			if (!$stmt->execute()){
				throw new Exception("не мога да взема юзер Id");
			};
			$userId = (int) $stmt->fetchColumn();

			//take company id
			$stmt = $this->conn->prepare("SELECT  `id`
										FROM `company`
										WHERE `name` =  ?");
			$stmt->bindValue (1,$companies[0]->getName());
			if (!$stmt->execute()){
				throw new Exception("не мога да взема компани Id");

			};
			$companyId = (int)$stmt->fetchColumn();

			//connect user with company
			$stmt = $this->conn->prepare("INSERT INTO `company_user`(`company_id`, `user_id`)
			                                     VALUES ( ? , ? )");
			
			$stmt->bindValue (2,$userId,PDO::PARAM_INT);
			$stmt->bindValue (1,$companyId,PDO::PARAM_INT);
			if (!$stmt->execute()){
				throw new Exception("не мога да добавя в юзер_компани");
			};

			//add shop
			$stmt = $this->conn->prepare("INSERT INTO `shop`( `company_id`, `delivery_id`, `name`, `address`, `phone`)
			                                        VALUES (?,99,?,?,?)");
			$stmt->bindValue (1,$companyId);
			$stmt->bindValue (2,$shops[0]->getName());
			$stmt->bindValue (3,$shops[0]->getAddress());
			$stmt->bindValue (4,$shops[0]->getPhone());
			if (!$stmt->execute()){
				throw new Exception ("не мога да добавя магазин");
			};

	}



}
?>